package cn.stronglink.assetmanage.business.inventory.view;

import android.app.Dialog;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.inventory.adapter.InventoryVpAdapter;
import cn.stronglink.assetmanage.entity.Inventory;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.widget.CustomProgressDialog;

/**
 * 操作入库主fragment
 * Created by chencongcong on 2018/6/12.
 */

public class InventoryView {
    private Fragment fragment;
    @BindView(R.id.tb_inventory)
    TabLayout tb_inventory;
    @BindView(R.id.vp_inventory)
    ViewPager vp_inventory;

    private Dialog mDialog;
    private String[] mTitle = {
            MyApplication.APP_CONTEXT.getResources().getString(R.string.ivventory_uncount),
            MyApplication.APP_CONTEXT.getResources().getString(R.string.ivventory_count),
            MyApplication.APP_CONTEXT.getResources().getString(R.string.check_wait_sync)
    };
    private InventoryVpAdapter vpAdapter;
    private FragmentManager fm;
    public InventoryView(Fragment fragment, View view) {
        try {
            this.fragment = fragment;
            ButterKnife.bind(this, view);

            mDialog = CustomProgressDialog.createLoadingDialog(fragment.getContext(), "正在加载中...");
            initView();
        }catch (Exception e){
            if(fragment!=null) Toast.makeText(fragment.getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

    private void initView() {
        fm=fragment.getChildFragmentManager();
//        fm = fragment.getActivity().getSupportFragmentManager();
        vpAdapter = new InventoryVpAdapter(fm,mTitle);
        vp_inventory.setAdapter(vpAdapter);
        tb_inventory.setupWithViewPager(vp_inventory);
        tb_inventory.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                vp_inventory.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 刷新屏幕中的数据
     */
    public void refreshView(){
        try {
            if (vpAdapter.getItem(vp_inventory.getCurrentItem()) instanceof IRefreshList) {
                ((IRefreshList) vpAdapter.getItem(vp_inventory.getCurrentItem())).refreshList();
            }
        }catch (Exception e){
            if(fragment!=null) Toast.makeText(fragment.getContext(),e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

    public void showProgressDialog(String msg){
        if (this.fragment != null){
            fragment.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mDialog.show();
                }
            });
        }

    }

    public void hideProgressDialog(){
        if (this.fragment != null){
            this.fragment.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mDialog.hide();
                }
            });
        }

    }

    public void showToast(String msg){
        ToastUtil.toastShort(msg);
    }
}
